# -*- encoding:utf-8 -*-
import time
import datetime
import json
import requests
from constant import constant_define

from dao.MySqlDAL import MySqlDAL
from config.Config import Config
import utils.CookieUtils as cu
__author__ = 'shudong.msd'

'''
    抓取各类组合热榜

    最热  0
    http://xueqiu.com/cubes/discover/rank/cube/list.json?category=14&count=1

    最赚钱

    收益高 111 月
    http://xueqiu.com/cubes/discover/rank/cube/list.json?category=12&count=10&market=cn&profit=monthly_gain

    涨跌快 112 月
    http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=monthly_gain&page=1&count=20

    收益高 121 日
    http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=best_benefit&category=12&profit=daily_gain&page=1&count=20

    涨跌快 122 日
    http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=daily_gain&page=1&count=20

    最人气 2
    http://xueqiu.com/cubes/discover/rank/cube/list.json?category=10&count=10&market=cn

    {"count":1,"filter":{"category":14},"page":1,"totalCount":193,
    "list":[

    {"id":12790,"name":"充电网络建设",
    "symbol":"ZH012890","market":"cn","daily_gain":-1.66,"monthly_gain":2.48,"total_gain":99.78,"net_value":1.9978,
    "annualized_gain_rate":99.78,"follower_count":2537,"style":{"name":"疯狂过山车"},"tag":["充电桩","电力设备"],
    "commission":6.0,
    }

    ],"maxPage":193}

'''

class CubeRankCrawler(object):

    # 每类组合找前100个当做样本
    __recordCount = 100

    # tablename
    __tablename_rank = 'tb_cube_rank'
    __tablename_info = 'tb_cube_info'

    __code_list = ['GB2312','GBK','utf-8','ascii','ANSI']

    __sqlUtil = MySqlDAL()

    __headers = {
            'Accept':'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding':'gzip, deflate, sdch',
            'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
            'cache-control':'no-cache',
            'Connection':'keep-alive',
            'User-Agent': constant_define.user_agent,
            'Host':'xueqiu.com',
            'X-Requested-With':'XMLHttpRequest',
            'Cookie': cu.init_cookie()
    }



    def init_target(self,target):
        self.__target = target
        self.__cube_list = self.__get_final_list()


    def set_cube_list(self,cube_list):
        self.__cube_list = cube_list

    def get_cube_list(self):
        return self.__cube_list

    # 得到响应信息
    def __get_response(self):
        return requests.get(self.__target,params={'count':self.__recordCount},headers=self.__headers).json()

    # 获得所有的数据dict
    def __get_final_list(self):
        res_dic = self.__get_response()
        res_list = res_dic['list']

        return res_list

    # 写入db rank
    def writeToDB_rank(self,type=0):
        cube_list = self.__cube_list

        # 插入rank
        keys_rank = ['symbol','type','rank_num','date','update_time']
        rank_num = 1
        source_rank = []
        # ttt = 0
        for row in cube_list:
            tmp = list()
            # if row['symbol'] == 'ZH210351':
            #     if ttt == 1:
            #         continue
            #     ttt += 1
            tmp.append(row['symbol'])
            tmp.append(type)
            tmp.append(rank_num)
            tmp.append(datetime.date.today())
            tmp.append(time.time())
            rank_num += 1
            source_rank.append(tmp)

        return self.__sqlUtil.insert_many_batch(keys_rank,source_rank,self.__tablename_rank,50)


    # 写入db info
    def writeToDB_info(self):
        cube_list = self.__cube_list

        # 插入info
        keys_info = ['symbol','daily_gain','monthly_gain','total_gain','annualized_gain_rate','net_value','follower_count','style','tag','date','update_time']
        source_info = []

        for row in cube_list:
            tmp = list()
            tmp.append(row['symbol'])
            tmp.append(row['daily_gain'])
            tmp.append(row['monthly_gain'])
            tmp.append(row['total_gain'])
            tmp.append(row['annualized_gain_rate'])
            tmp.append(row['net_value'])
            tmp.append(row['follower_count'])
            tmp.append(row['style']['name'])
            tmp.append(json.dumps(row['tag'],encoding='utf-8'))
            tmp.append(datetime.date.today())
            tmp.append(time.time())
            source_info.append(tmp)

        return self.__sqlUtil.insert_many_batch(keys_info,source_info,self.__tablename_info,50)

    # 更新db
    def updateDB_rank(self,type=0):
        # data_source=[(修改值1),(2),(3)...(过滤条件1),(2)...] ,
        cube_list = self.__cube_list
        # 插入rank
        keys_rank = ['rank_num','update_time']
        filter_keys_rank=['symbol','date','type']
        rank_num = 1
        source_rank = []
        for row in cube_list:
            tmp = list()
            tmp.append(rank_num)
            tmp.append(time.time())

            tmp.append(row['symbol'])
            tmp.append(datetime.date.today())
            tmp.append(type)

            rank_num += 1
            source_rank.append(tmp)

        return self.__sqlUtil.update_many_batch(keys_rank,source_rank,self.__tablename_rank,filter_keys_rank,50)


    def updateDB_info(self):
        # 插入info
        cube_list = self.__cube_list
        keys_info = ['daily_gain','monthly_gain','total_gain','annualized_gain_rate','net_value','follower_count','style','tag','update_time']
        source_info = []
        filter_keys_info = ['symbol','date']
        for row in cube_list:
            tmp = list()
            tmp.append(row['daily_gain'])
            tmp.append(row['monthly_gain'])
            tmp.append(row['total_gain'])
            tmp.append(row['annualized_gain_rate'])
            tmp.append(row['net_value'])
            tmp.append(row['follower_count'])
            tmp.append(row['style']['name'])
            tmp.append(json.dumps(row['tag'],encoding='utf-8'))
            tmp.append(time.time())

            tmp.append(row['symbol'])
            tmp.append(datetime.date.today())
            source_info.append(tmp)

        return self.__sqlUtil.update_many_batch(keys_info,source_info,self.__tablename_info,filter_keys_info,50)
